package gui.common;

import gui.ButtonName;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import data.Forum;
import data.Group;
import data.Message;

public class MessageListPanel extends JPanel{
private static final long serialVersionUID = 1L;
	
	//top
	private JTextField txtForumName;
	private JTextField txtForumCreater;
	private JTextField txtForumDate;
	
	private JTextArea txtForumMessage;
	//center
	private JList messageList;
	protected JScrollPane scroll;
	//right
	private JButton btnViewMessage;
	private JButton btnAddMessage;
	private JButton btnLeaveForum;
	private JButton btnDeleteMessage;
	
	private JPanel topPanel;
	private JPanel rightPanel;
	private JPanel centerPanel;
	
	private LayoutManager topLayout=null;
	private LayoutManager rightLayout=null;
	private LayoutManager centerLayout=null;
	
	private ActionListener btnListener=null;
	private List<Message> messageData=null;
	private Forum forum=null;
	
	private Message currentMessage=null;
	public Message getCurrentMessage() {
		return currentMessage;
	}

	public MessageListPanel(Forum forum,List<Message> data,ActionListener listener){
		this.messageData=data;
		this.btnListener=listener;
		this.forum=forum;
		initUI();
	}
	
	protected void initUI(){
		this.setLayout(new BorderLayout());
		initTop();
		initRight();
		initCenter();
		this.add(topPanel,BorderLayout.NORTH);
		this.add(centerPanel,BorderLayout.CENTER);
		this.add(rightPanel,BorderLayout.EAST);
		
	}
	
	protected void initTop(){
		topPanel=new JPanel();
		
		txtForumName=new JTextField(forum.getSubject());
		txtForumName.setBorder(BorderFactory.createTitledBorder("Forum Subject"));
		txtForumName.setEditable(false);
		
		txtForumCreater=new JTextField(forum.getUserName());
		txtForumCreater.setBorder(BorderFactory.createTitledBorder("Creater Name"));
		txtForumCreater.setEditable(false);
		txtForumDate=new JTextField(forum.getDatetimeString());
		txtForumDate.setBorder(BorderFactory.createTitledBorder("Date Time"));
		txtForumDate.setEditable(false);
		
		txtForumMessage=new JTextArea(forum.getMessage());
		txtForumMessage.setBorder(BorderFactory.createTitledBorder("Message"));
		txtForumMessage.setEditable(false);
		
		topPanel.setLayout(new GridLayout(2,1));
		
		JPanel up=new JPanel();
		up.setLayout(new GridLayout(1,3));
		up.add(txtForumName);
		up.add(txtForumCreater);
		up.add(txtForumDate);
		
		topPanel.add(up);
		topPanel.add(txtForumMessage);
		
		 
         
	}
	
	protected void initCenter(){
		centerPanel=new JPanel();
		centerPanel.setBorder(BorderFactory.createTitledBorder("Message List Info"));
		centerLayout=new GridLayout(1,1);
		//forumData
		Vector<Message> data=new Vector<Message>();
		for (Message message : messageData) {
			data.add(message);
		}
		messageList=new JList(data);
		messageList.addMouseListener(new MouseAdapter(){
			public void mouseClicked(MouseEvent e) {
				int index = messageList.locationToIndex(e.getPoint());
		         currentMessage=messageData.get(index);
		         System.out.println("Double clicked on Item " + currentMessage);
			}
			
		});
		scroll=new JScrollPane();
		scroll.setViewportView(messageList);
		centerPanel.add(scroll);
		
		
	}
	protected void initRight(){
		rightPanel=new JPanel();
		rightPanel.setBorder(BorderFactory.createTitledBorder("Avilable Operation"));
		
		rightLayout=new GridLayout(8,1);
		((GridLayout)rightLayout).setVgap(10);
		
		rightPanel.setLayout(rightLayout);
		
		btnViewMessage=new JButton(ButtonName.VIEW_MESSAGE);
		btnViewMessage.addActionListener(btnListener);
		btnLeaveForum=new JButton(ButtonName.LEAVE_FORUM);
		btnLeaveForum.addActionListener(btnListener);
		btnAddMessage=new JButton(ButtonName.CREATE_NEW_MESSAGE);
		btnAddMessage.addActionListener(btnListener);
		
		btnDeleteMessage=new JButton(ButtonName.DELETE_MESSAGE);
		btnDeleteMessage.addActionListener(btnListener);
		btnDeleteMessage.setVisible(false);
		
		rightPanel.add(btnLeaveForum);
		rightPanel.add(btnViewMessage);
		rightPanel.add(btnAddMessage);
		rightPanel.add(btnDeleteMessage);
		
		
	}

	
	public void showAdminButtons(){
		btnDeleteMessage.setVisible(true);
	}
	
	public void showUserButttons(){
		
	}
	
	public static void main(String[] args) {
		Forum d=new Forum();
		d.setId(1);
		d.setUserName("ym221479");
		d.setSubject("help");
		d.setMessage("One common use for tModel entities is to represent technical specifications or concepts. For example, a tModel can be used to represent a specification that defines wire protocols,");
		d.setTime(new Date(System.currentTimeMillis()));
		Message msg=new Message();
		
		msg.setSubject("HiFi Group");
		msg.setUserName("scm");
		msg.setTime((new Date(System.currentTimeMillis()-90*24*60*60000)));
		msg.setMessage("a group for any one like hifi\nant one");
		List<Message> data=new ArrayList<Message>();
		data.add(msg);
		
		JPanel ss=new MessageListPanel(d,data,null);
		new gui.server.TestPanel(ss);
	}
}
